﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX.Direct3D10;
using System.Windows.Media.Imaging;
using System.IO;

namespace System.Windows.Media.Media3D10
{
    /// <summary>
    /// Classe Helper pour les <see cref="EffectResourceVariable"/>
    /// </summary>
    public static class EffectResourceVariableHelper
    {
        /// <summary>
        /// Affecte une <see cref="Texture2D"/> à un <see cref="EffectResourceVariable"/>.
        /// </summary>
        /// <param name="effectResource"><see cref="EffectResourceVariable"/> à initialiser.</param>
        /// <param name="brush"><see cref="Brush"/> à utiliser pour générer la texture.</param>
        public static void SetTexture(this EffectResourceVariable effectResource, Brush brush)
        {
            if (object.ReferenceEquals(effectResource, null))
                throw new ArgumentNullException("effectResource");

            using (Texture2D texture = brush.ToTexture2D())
            {
                effectResource.SetTexture(texture);
            }
        }

        /// <summary>
        /// Affecte une <see cref="Texture2D"/> à un <see cref="EffectResourceVariable"/>.
        /// </summary>
        /// <param name="effectResource"><see cref="EffectResourceVariable"/> à initialiser.</param>
        /// <param name="source"><see cref="BitmapSource"/> à utiliser pour générer la texture.</param>
        public static void SetTexture(this EffectResourceVariable effectResource, BitmapSource source)
        {
            if (object.ReferenceEquals(effectResource, null))
                throw new ArgumentNullException("effectResource");
            if (object.ReferenceEquals(source, null))
                throw new ArgumentNullException("source");

            using(Texture2D texture = source.ToTexture2D())
            {
                effectResource.SetTexture(texture);
            }
        }

        /// <summary>
        /// Affecte une <see cref="Resource"/> à un <see cref="EffectResourceVariable"/>.
        /// </summary>
        /// <param name="effectResource"><see cref="EffectResourceVariable"/> à initialiser.</param>
        /// <param name="texture"><see cref="Resource"/> à affecter</param>
        public static void SetTexture(this EffectResourceVariable effectResource, Resource texture)
        {
            if (object.ReferenceEquals(effectResource, null))
                throw new ArgumentNullException("effectResource");

            ShaderResourceView sRV = null;
            try
            {
                sRV = object.ReferenceEquals(texture, null) ? null : new ShaderResourceView(Engine.Device10, texture);
                effectResource.SetResource(sRV);
            }
            finally
            {
                if (sRV != null)
                {
                    sRV.Dispose();
                    sRV = null;
                }
            }
        }
    }
}
